(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a) (sum term (next a) next b))))

(define (integral f a b dx)
  (define (add-dx x) (+ x dx))
  (* (sum f (+ a (/ dx 2.0)) add-dx b)
     dx))

(define (cube n) (* n n n))

(define (Simpson f a b n)
    (define h (/ (- b a) n))
    (define (y k)
        (f (+ a (* k h))))
    (define (factor k)
        (cond   ((OR (= k 0) (= k n)) 1)
                ((even? k) 2)
                (else 4)))
    (define (term k)
        (* (factor k) (y k)))

    (define (next k)
        (+ k 1))

    (* (/ h 3.0) (sum term a next n)))

(display "\n========================================\n")
(display (integral cube 1 2 0.1))
(newline)
(display (Simpson cube 1 2 10))
(display "\n========================================\n")
